﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using HKEA.Exporter.Models;
using HKEA.Exporter.Utility;
namespace HKEA.Exporter.DAL
{
    public class ActivityDAL
    {
        private const string strSQL = "select id,code,category,title,title_cht,startdate,enddate from t_news where publish='YES' and category=@code";
        public List<Models.Activity> GetActivityList(string typecode)
        {
            SqlParameter param = new SqlParameter("@code", SqlDbType.NVarChar, 50);
            param.Value = typecode;
            var list = new List<Models.Activity>();
            int id;
            string code, category, title, title_cht;
            DateTime startdate, enddate;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.ConnectionStringLocalTransaction, CommandType.Text, strSQL, param))
            {
                while (dr.Read())
                {
                    id = Convert.ToInt32(dr["id"]);
                    code = dr[1]==null ? string.Empty : dr[1].ToString();
                    category = dr[2] == null ? string.Empty : dr[2].ToString();
                    title = dr[3] == null ? string.Empty : dr[3].ToString();
                    title_cht = dr[4] == null ? string.Empty : dr[4].ToString();
                    startdate = dr[5] == null ? DateTime.Now : (DateTime)dr[5];
                    enddate = dr[6] == null ? DateTime.Now : (DateTime)dr[6];
                    list.Add(new Activity(id, code, category, title_cht, title, startdate, enddate));
                }
            }
            return list;
        }
    }
}
